#include <stdio.h>
#include <stdlib.h>
#include "duoxiancheng.h"
#include <pthread.h>
#include <time.h>
#include <string.h>

void free_code(){
	for(int v =0;v<256;++v){
		if(code[v])
			free(code[v]);
	}

}

void protime(void (*p)(char**,int),char **args,int len){
	clock_t start,end;
	start = clock();
	p(args,len);
	end = clock();
	printf("用时%lf秒\n",(double)(end-start)/CLOCKS_PER_SEC);
}

void show(char **args,int len){
	if((fg = fopen(args[0],"rb"))==NULL){
		printf("%s ",args[0]);
		perror("打开文件错误");
		exit(1);
	}
	fseek(fg,0,SEEK_END);
	size = ftell(fg);
	rewind(fg);
	in_open(fg);
	printf("共%d个文件\n",namelen);
	for(int i=0;i<namelen;++i){
		printf("%s %ld\n",fgs[i].name,fgs[i].len);
	}
}

void oin(char **args,int len){
	int i,k=65,l;
	fhanshu fhan = {0};
	if((fg = fopen(args[0],"rb"))==NULL){
		printf("%s ",args[0]);
		perror("打开文件错误");
		exit(1);
	}
	inname = (char *)malloc(strlen(args[0]+1));
	strcpy(inname,args[0]);
	fseek(fg,0,SEEK_END);
	size = ftell(fg);
	rewind(fg);
	in_open(fg);
	fhan.iohp = hafin;
	th = 1;
	caozuo(pinduqu,&fhan);
	}
void ducode(char *str){
	fhanshu fhan;
	if((fg = fopen(str,"rb"))==NULL){
		printf("%s",str);
		perror("打开文件错误");
		exit(1);
	}

	th = 2;
	fhan.hp = hafdu;
	fhan.fp = fg;
	fseek(fg,0,SEEK_END);
	size = ftell(fg);
	rewind(fg);
	caozuo(duqu,&fhan);
	rewind(fg);
}

void pinjie(){
	FILE *fg;
	fhanshu fhan = {0};
	for(int i=0;i<namelen;++i){
		if((fg = fopen(outname[i],"rb"))==NULL){
			perror("打开文件错误");
			exit(1);
		}
		fseek(fg,0,SEEK_END);
		size = ftell(fg);
		rewind(fg);
		fhan.hp = filepinjie;
		fhan.fp = fg;
		fileduqu(&fhan);
		remove(outname[i]);
	}
}

void out(char **args,int len){
	fhanshu fhan;
	bao fbao = {0};
	FILE *file;
	char *cpp;
	fgs = (bao*)malloc(sizeof(bao)*(len)-1);
	namelen = len-1;
	outname = (char**)malloc(len-1);
	for(int i=0;i<=len-2;++i){
		ducode(args[i]);
		if((cpp=strrchr(args[i],'/'))){
			args[i] = cpp+1;
		}
		char *str = (char*)malloc(strlen(args[i])+8);
		strcpy(str,args[i]);
		strcat(str,".hafout");
		outname[i] = str;
		file = out_open(str);
		fbao.fg = fg;
		fbao.ofg = file;
		fbao.name = args[i];
		fbao.len = size;
		fgs[i] = fbao;
	}
	hafcode(arr,256);
	fhan.iohp = hafout;
	th = 1;
	size = 0;
	caozuo(pthduqu,&fhan);
	namelen = len-1;
	file = out_open(args[len-1]);
	in = file;
	for(int i=0;i<namelen;++i){
		if((fg = fopen(outname[i],"rb"))==NULL){
			perror("打开文件错误");
			exit(1);
		}
		fseek(fg,0,SEEK_END);
		size = ftell(fg);
		rewind(fg);
		fgs[i].len = size;
	}
	xietou(file);
	pinjie();
	free_code();

}

int main(int arg,char *args[]){
	code_tab  = (unsigned char***)calloc(65,sizeof(fg));
	lcode_tab = (long_code***)calloc(256,sizeof(fg));
	pthread_mutex_init(&mutex,NULL);
	pthread_mutex_init(&mutexr,NULL);
	pthread_mutex_init(&du_mutex,NULL);
	pthread_mutex_init(&xie_mutex,NULL);
	pthread_cond_init(&cond,0);
	pthread_cond_init(&xcond,0);
	if(arg == 1){
		perror("!=1");
		exit(1);
	}

	if(strchr(args[1],'-')){
		if(strchr(args[1],'o') && !strchr(args[1],'u')){
			protime(out,args+2,arg-2);
		}
		else if(strchr(args[1],'u') && !strchr(args[1],'o')){
			protime(oin,args+2,arg-2);
		}
		else if(*(args[1]+1)=='l'){
			protime(show,args+2,arg-2);
		}else if(strcmp("--help",args[1])==0){
			printf("\n%s\n\n","本软件是基于哈夫曼编码的压缩工具。");
			printf("%s\n\n","命令格式：hafya   -选项   [文件名（多个文件名用空格分开，解压时只能输入一个文件）]   [输出的文件名（解压时不需要此参数）]");
			printf("%s\n\n","选项：");
			printf("%s\n"," -o	压缩");
			printf("%s\n"," -u	解压");
			printf("%s\n"," -l	查看压缩包内容");
			printf("%s\n"," -p	加密和解密（此功能暂未开放）");
			printf("%s\n\n"," --help	查看帮助");
			printf("%s\n\n","18网络工程刘帅");
		}


	}else{
		perror("命令格式错误");
		exit(1);
	}
	pthread_mutex_destroy(&mutex);
	pthread_mutex_destroy(&mutexr);
	pthread_mutex_destroy(&du_mutex);
	pthread_mutex_destroy(&xie_mutex);
	pthread_cond_destroy(&cond);
	pthread_cond_destroy(&xcond);
	return 0;
}
